package com.medicine.hbp.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.HomeAdapter;
import com.medicine.hbp.adapter.ImageFragmentAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.AdlnfoListVo;
import com.medicine.hbp.mob.AdlnfoVo;
import com.medicine.hbp.mob.laberInfoListVo;
import com.medicine.hbp.mob.laberInfoVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.DisplayUtil;
import com.medicine.hbp.uitl.GlideImageLoader;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

/**
 * 首页
 * @author Administrator
 *
 */
public class HomeActivity extends BaseActivity implements IXListViewListener,OnItemClickListener {
	@BindView(id = R.id.xListView) public XListView xListView;
	@BindView(id = R.id.txt_yg_num) public TextView txt_yg_num;// 总数
	@BindView(id = R.id.img_fg, click = true) public ImageView img_fg;// 投稿点击

	private Banner banner;// 投稿点击
	private HomeAdapter adapter;
	private View v;

	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_home);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		adapter = new HomeAdapter(aty);
		xListView.setAdapter(adapter);
		xListView.setOnItemClickListener(this);
		xListView.setXListViewListener(this);
		v = LayoutInflater.from(aty).inflate(R.layout.banner_classification,null);
		banner = (Banner) v.findViewById(R.id.banner);
		banner.setVisibility(View.GONE);
		xListView.addHeaderView(v);
		DadosSp.setAdapterOne(aty, adapter);
		txt_yg_num.setText(DadosSp.getHomeSzie(aty));

		httpLaberGroupList();
//		initViewPage(null);
		httpBanner();
	}

	/**
	 * 首页广告图片获取
	 */
	private void httpBanner() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.ADLIST, params,
				new AppHttpCallBack<AdlnfoListVo>(AdlnfoListVo.class, aty) {
					@Override
					public void onSuccess(AdlnfoListVo result) {
						if (result != null && result.adInfo.size() > 0) {
							initViewPage(result.adInfo);
						}
//						else{
//							images.add("http://f11.baidu.com/it/u=1277336972,1914777930&fm=72");
//							images.add("http://img4.imgtn.bdimg.com/it/u=1665056862,4010355512&fm=26&gp=0.jpg");
//						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
	List<String> images=new ArrayList<>();
	/**
	 * 分类banner展示
	 */
	private void initViewPage(final List<AdlnfoVo> adInfo) {
		for (int i=0;i<adInfo.size();i++){
			images.add(adInfo.get(i).adImg);
		}
//		images.add("http://f11.baidu.com/it/u=1277336972,1914777930&fm=72");
//		images.add("http://img4.imgtn.bdimg.com/it/u=1665056862,4010355512&fm=26&gp=0.jpg");
		//设置banner样式
		banner.setVisibility(View.VISIBLE);
		banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
		//设置图片加载器
		banner.setImageLoader(new GlideImageLoader());
		//设置图片集合
		banner.setImages(images);
		//设置轮播时间
		banner.setDelayTime(1500);
		//设置自动轮播，默认为true
		banner.isAutoPlay(true);
		banner.setOnBannerClickListener(new OnBannerClickListener() {
			@Override
			public void OnBannerClick(int i) {
				/**
				 * 广告链接
				 * 16001  更多精彩应用
				 * 16101 近期热点1
				 * 16102 近期热点2
				 * 16103 近期热点3
				 * 16201 专题1
				 * 16202 专题2
				 * 16203 专题3
				 * 16204 专题4
				 * 16301 关心
				 * 16401 投票
				 * 16501 积分活动
				 */
				if(adInfo.get(i-1).adType==1){
					Intent intent=new Intent(aty,BannerWebActivity.class);
					if(TextUtils.isEmpty(LoginSp.getToken(aty))){
						intent.putExtra("URL",adInfo.get(i-1).adUrl);
					}else{
						intent.putExtra("URL",adInfo.get(i-1).adUrl+"&token="+LoginSp.getToken(aty));
					}
					intent.putExtra("NAME",adInfo.get(i-1).adName);
					aty.startActivity(intent);
				}else{
					Intent intent=null;
					if(adInfo.get(i-1).adUrl.equals("16101")){//近期热点1
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "1");
						intent.putExtra("name", "近期热点1");
						intent.putExtra("type", 1);
					}else if(adInfo.get(i-1).adUrl.equals("16102")){//近期热点2
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "2");
						intent.putExtra("name", "近期热点2");
						intent.putExtra("type", 1);
					}else if(adInfo.get(i-1).adUrl.equals("16103")){//近期热点3
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "3");
						intent.putExtra("name", "近期热点3");
						intent.putExtra("type", 1);
					}else if(adInfo.get(i-1).adUrl.equals("16201")){//专题1
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "1");
						intent.putExtra("name", "专题1");
						intent.putExtra("type", 2);
					}else if(adInfo.get(i-1).adUrl.equals("16202")){//专题2
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "2");
						intent.putExtra("name", "专题2");
						intent.putExtra("type", 2);
					}else if(adInfo.get(i-1).adUrl.equals("16203")){//专题3
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "3");
						intent.putExtra("name", "专题3");
						intent.putExtra("type", 2);
					}else if(adInfo.get(i-1).adUrl.equals("16204")){//专题4
						intent=new Intent(aty,HotspotActivity.class);
						intent.putExtra("id", "4");
						intent.putExtra("name", "专题4");
						intent.putExtra("type", 2);
					}else if(adInfo.get(i-1).adUrl.equals("16001")){//更多精彩应用
						intent=new Intent(aty,WonderfulAppActivity.class);
					}else if(adInfo.get(i-1).adUrl.equals("16301")){//关心
						MainActivity.setTab(1);
					}else if(adInfo.get(i-1).adUrl.equals("16401")){//投票
						MainActivity.setTab(2);
					}else if(adInfo.get(i-1).adUrl.equals("16501")){//积分活动
						intent=new Intent(aty,IntegralActivity.class);
					}
					if(intent!=null){
						aty.startActivity(intent);
					}
				}
			}
		});
		//banner设置方法全部调用完毕时最后调用
		banner.start();

	}
	private List<laberInfoVo> laberInfoVos;

	/**
	 * 首页标签组获取
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(aty,HttpInterface.LABER_GROUP_LIST,params,new AppHttpCallBack<laberInfoListVo>(laberInfoListVo.class, aty) {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					JSONObject object = new JSONObject(t);
					txt_yg_num.setText("共收录"+ object.getString("laberNumber")+ "种治疗方案");
					DadosSp.setHomeSzie(aty, txt_yg_num.getText().toString());
//							if(TextUtils.isEmpty(object.getString("hots"))){
//								adapter.setJqrd("");
//							}else{
//								adapter.setJqrd(object.getString("hots"));
//							}

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onSuccess(laberInfoListVo result) {
				try {
					boolean hasMore = true;
					laberInfoVos = result.laberInfo;
					if (refresh) {
						DadosSp.setTabOne(aty, gson.toJson(result));
						adapter.changeData(result.laberInfo);
						adapter.setI(0);
					} else {
						adapter.addItems(result.laberInfo);
						adapter.notifyDataSetChanged();
					}
					if (result.laberInfo.size() < pageSize) {
						hasMore = false;
//								TextView view=new TextView(aty);
//								view.setText("没有更多内容了");
//								xListView.addFooterView(view);
					}
					xListView.setPullLoadEnable(hasMore);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}

			@Override
			public void onFailures(int errorCode, String errorMsg) {
				ToastUtil.showShort(aty, errorMsg);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				xListView.setRefreshTime(CommUtil.formatDate(System
						.currentTimeMillis()));
			}
		});
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		if(TextUtils.isEmpty(LoginSp.getToken(aty))){
			StartIntent.startLogin(aty);
		}else{
			StartIntent.startRelease(aty);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	public void onRefresh() {
		page = 1;
		refresh = true;
		adapter.setI(0);
		xListView.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}

}
